
/**
 *	js validata
 *	author leeshaoqun
 *	date 2009-11-7 14:23
 *	
 *	ÐèÒªjqueryÖ§³Ö
 *	Ö§³Ö integer,float,munber(integer+flaot),empty String µÄÑéÖ¤
 *	±íµ¥¿Ø¼þµÄidºÍclassÊôÐÔÊÇ±ØÐëµÄ
 *	class : validata_integer,validata_float,validata_number,validata_empty
 *	id : ×Ô¶¨Òå(×÷Îª´íÎóÐÅÏ¢Êý×éµÄkey)
 *	<input type="text" name="t1" id="t1" class="validata_integer" size="10"/>
 *	
 *	Ò³Ãæ´æ·Å´íÎóÐÅÏ¢±êÇ©
 *	<div id="error"></div>
 *
 *
 *	//ÅäÖÃ´íÎóÊä³öÐÅÏ¢Êý×é,key±ØÐëºÍ±íµ¥¿Ø¼þµÄid¶ÔÓ¦
 *	var errorInfo = new Array();
 *	errorInfo['t1'] = "ÇëÌîÐ´Êý×Ö1";
 *
 *	var validata = new Validata();//ÊµÀý»¯Ð£Ñé¶ÔÏó
 *	validata.initErrorInfo(errorInfo);//³õÊ¼»¯´íÎóÐÅÏ¢
 *	validata.check();//Ö´ÐÐ¼ì²é£¬return boolean
 *
 **/
var ValidataObject = {};


ValidataObject.INIT_ERROR_MSG = "error!";
ValidataObject.ERROR_BACKGROUD_COLOR = "#ffff00";


ValidataObject.isEmpty = function(obj){

	if(obj == ""){
		return false;
	}else{     
       return true;
    }
}


ValidataObject.isInteger = function(obj){
	 var reg=/^[-+]?\d+$/;  
  // var reg=/^\d+$/;      
   if(!reg.test(obj)){     
         return false;
	}else{     
       return true;
    }  
}

ValidataObject.isFloat = function(obj){
	//var reg = /^[-+]?\*\.?\d+$/;
	var reg = /^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/;
	if(!reg.test(obj)){     
         return false;
	}else{     
       return true;
    } 

}

ValidataObject.isNumber = function(obj){
//	var regex = /^\d*\.?\d*$/;
//    if (!regex.test(obj)){
//        return false;
//    }else{
//    return true;
//    }
	return this.isInteger(obj) ||  this.isFloat(obj);
}


//³õÊ¼»¯´íÎóÏÔÊ¾ÐÅÏ¢,Íâ²¿´«Èë´íÎóÐÅÏ¢Êý×é
//Array:key=id,value=error_msg
ValidataObject.errorInfoArray = new Array();

ValidataObject.initErrorInfo = function(){
	this.errorInfoArray['empty'] = "不能为空!";
	this.errorInfoArray['integer'] = "只能填整数!";
	this.errorInfoArray['float'] = "只能填小数!";
	this.errorInfoArray['number'] = "只能填数字!";
}

ValidataObject.getErrorInfo = function(){
	return this.errorInfoArray;
}


ValidataObject.getCheckType = function(obj,type){
	if(type == "integer"){
		return this.isInteger(obj);
	}else if(type == "float"){
		return this.isFloat(obj);
	}else if(type == "number"){
		return this.isNumber(obj);
	}else{
		return this.isEmpty(obj);
	}
}


//classname=validata_integer,validata_float,validata_number,validata_empty
//type=integer,float,munber,empty
ValidataObject.checkdata = function(classname,type){
	var validateObject = this;
	var flag = true;
	
	var tags = $("."+classname);
	if(tags.length < 1){
		return true;
	}

	tags.each(function(){		
		var obj = $(this);
		if(!validateObject.getCheckType($(this).val(),type)){
			var errors = validateObject.getErrorInfo();
			//obj.css("background",validateObject.ERROR_BACKGROUD_COLOR);
			$("#info_" + obj.attr("name")).html(errors[type]);
			flag = false;
		}else{
			//obj.css("background","");
			$("#info_" + obj.attr("name")).html("");
		}
	});

	return flag;
}



//Íâ²¿µ÷ÓÃ
ValidataObject.check = function(){
	
	if(!this.checkdata("validata_empty","empty")){
		return false;
	}
	
	if(!this.checkdata("validata_integer","integer")){
		return false;
	}
	
	if(!this.checkdata("validata_float","float")){
		return false;
	}

	if(!this.checkdata("validata_number","number")){
		return false;
	}

	return true;
}


var Validata = new Function();
Validata.prototype = ValidataObject;












